﻿using DLL.NET.BindingInjector;
using DLL.NET70.Infrastructure.Prism.MVVM;
using Microsoft.Win32;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace Module.BindingInjector.ViewModels
{
    public class PUC_InjectorAuthorizeViewModel : CPDMVVMBase
    {
        private string _scanBar = "";

        public string ScanBar
        {
            get { return _scanBar; }
            set { SetProperty(ref _scanBar, value); }
        }

        private bool _barFocus = false;

        public bool BarFocus
        {
            get { return _barFocus; }
            set
            {
                SetProperty(ref _barFocus, value);
            }
        }

        private ObservableCollection<string> _segment = new ObservableCollection<string>();

        public ObservableCollection<string> Segment
        {
            get { return _segment; }
            set
            {
                SetProperty(ref _segment, value);
            }
        }

        public PUC_InjectorAuthorizeViewModel(IRegionManager regionManager, IEventAggregator eventAggregator) : base(regionManager, eventAggregator)
        {
            for (int i = 0; i < 8; i++)
            {
                Segment.Add("");
            }
        }

        public override void Loaded()
        {
            BarFocus = true;
            base.Loaded();
        }

        private int UseCount = 0;

        public override void OnDialogOpened(IDialogParameters parameters)
        {
            //base.OnDialogOpened(parameters);
            UseCount = parameters.GetValue<int>("Value");
        }

        //EnterKeyCommand
        public DelegateCommand EnterKeyCommand
        {
            get { return new DelegateCommand(EnterKey); }
        }

        private int count = 0;

        private void EnterKey()
        {
            try
            {
                Register();
            }
            catch (Exception e)
            {
                Message = "数据验证出现异常:" + e.Message;
            }

            ScanBar = "";
            //设置焦点
            BarFocus = true;
        }

        public DelegateCommand RegisterCommand
        {
            get { return new DelegateCommand(Register); }
        }

        private void Register()
        {
            //第一步：是加密后的字符串，先解密
            try
            {
                string date = DateTime.Now.ToString("yyyy-MM-dd");
                string jdd = CJiaMi.Decrypt(ScanBar, "12345678");
                string[] sbar = jdd.Split("_");
                string gdate = sbar[0];
                string gcount = sbar[1];
                string mm = sbar[0].Substring(2, 2);
                string dd = sbar[0].Substring(6, 2);

                string[] sd = date.Split("-");

                if ((mm == sd[1]) && (dd == sd[2]))
                {
                    //将数据保存到本地，并发送数据保存成功的消息
                    int AllowCount = Convert.ToInt32(gcount);
                    bool ret = WR_CInjectorCount.Save(AllowCount, 0);
                    if (false == ret)
                    {
                        Message = "注册失败！";
                    }
                    else
                    {
                        Message = "注册成功";
                        string v = AllowCount.ToString() + "-0";
                        SendCommandMessage("RegisterInjectorOk", v);
                    }
                }
                else
                {
                    Message = "注册码无效";
                }
            }
            catch (Exception e)
            {
                Message = "注册出现异常：" + e.Message;
            }
        }

        public DelegateCommand Register1Command
        {
            get { return new DelegateCommand(Register1); }
        }

        private void Register1()
        {
            ScanBar = Segment[0] + Segment[1] + Segment[3] + Segment[4] + Segment[5] + Segment[6] + Segment[7];
            Register();
        }

        public DelegateCommand CloseCommand
        {
            get { return new DelegateCommand(Close); }
        }

        private void Close()
        {
            ButtonResult result = ButtonResult.No;
            RaiseRequestClose(new DialogResult(result));
        }
    }
}